package com.yupi.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yupi.model.entity.InterfaceInfo;
import com.yupi.model.entity.UserInterfaceInfo;
import com.yupi.project.common.BaseResponse;
import com.yupi.project.common.ResultUtils;
import com.yupi.project.mapper.InterfaceInfoMapper;
import com.yupi.project.mapper.UserInterfaceInfoMapper;
import com.yupi.project.model.vo.InterfaceInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/analysis")
@Slf4j
public class AnalysisController {


    /**
     * 业务逻辑较为简单，没有service，直接在controller中写
     * @return
     */

    @Resource
    private UserInterfaceInfoMapper userInterfaceInfoMapper;

    @Resource
    private InterfaceInfoMapper interfaceInfoMapper;


    @GetMapping("top/interface/invoke")
    public BaseResponse<List<InterfaceInfoVO>> listTopInvokeInterfaceInfo(){
        //1: 从userInterfaceinfo表中查出 UserInterfaceInfo列表
        int limit = 3;//这个其实可以是前端传来的参数，不过我前端不会写，我先把这个limit写死
        List<UserInterfaceInfo> userInterfaceInfos = userInterfaceInfoMapper.listTopInvokeInterfaceInfo(limit);
        //2：再根据这个列表中的interfaceinfoId字段查出 InterfaceInfo
        ArrayList<Long> interfaceinfoIdList = new ArrayList<>();
        final ArrayList<InterfaceInfoVO> interfaceInfoVOList = new ArrayList<>();
        for (UserInterfaceInfo userInterfaceInfo : userInterfaceInfos) {
            final InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
            interfaceInfoVO.setNum(userInterfaceInfo.getTotalNum());
            interfaceInfoVOList.add(interfaceInfoVO);
            interfaceinfoIdList.add(userInterfaceInfo.getInterfaceInfoId());
        }
        LambdaQueryWrapper<InterfaceInfoVO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        final LambdaQueryWrapper<InterfaceInfoVO> eq =
                lambdaQueryWrapper.eq(InterfaceInfoVO::getId, interfaceinfoIdList);
        final List<InterfaceInfo> interfaceInfoList = interfaceInfoMapper.selectBatchIds(interfaceinfoIdList);
        for (int i = 0; i < interfaceInfoList.size(); i++) {
            BeanUtils.copyProperties(interfaceInfoList.get(i),interfaceInfoVOList.get(i));
        }
        return ResultUtils.success(interfaceInfoVOList);
    }
}
